module.exports = {
  
  // 处理模态框滚动穿透问题
  catchtouchmove() {},

  // 获取当前时间戳
  getTime() {
    return new Date().getTime()
  },
  
  // toast提示
  showToast(option = {}) {
    if (typeof option === 'string') {
      wx.showToast({
        title: option,
        icon: 'none'
      })
    } else {
      let { title, icon = 'none', duration = 1500, success } = option
      wx.showToast({
        title,
        icon,
        duration,
        success: (res) => {
          setTimeout(() => {
            success && success.call(this, res)
          }, duration)
        }
      })
    }
  },
  
  // url加密
  encodeURIComponent(url) {
    return encodeURIComponent(url)
  },
  
  // url解码
  decodeURIComponent(url) {
    return decodeURIComponent(url)
  },
  
  // 跳转页面
  gotoPage(e) {
    let { url, type = 'navigateTo' } = e.currentTarget.dataset
    wx[type]({
      url
    })
  },

  /**
   * 补零方法
   * TODO 只支持补一个零，小于10的补零，其余都不补
   */
  zerofill(n) {
    return n >= 10 ? String(n) : '0' + n
  },

  /**
   * 生成一个uuid
   */
  uuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
      var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
      return v.toString(16)
    })
  },

  // 格式化字符
  format() {
    let str = arguments[0]
    if (typeof (str) == 'undefined' || str == null || str == '' || str == 'undefined') return str
    for (let i = 1; i < arguments.length; i++) {
      let re = new RegExp('\\{' + (i - 1) + '\\}', 'gm')
      str = str.replace(re, arguments[i])
    }
    return str
  }

}